<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<?php
/* @var $this MemberController */
/* @var $model Member */
$this->layout = "//layouts/column1";
if (Yii::app()->user->isAdmin()) {
    $user = Member::model()->findByAttributes(array('username' => 'admin'));
} else {
    //$user = Member::model()->findByAttributes(array('member_id' => Yii::app()->user->id));
    $member_id = Yii::app()->user->id;
}

function getBalanceStatus($type, $member_id) {
    if($type == 'REFERRAL'){
        $column = "credit";
        $condition = "and isReferralEarning='1'";
    }
    if($type == 'VIDEO'){
        $column = "credit";
        $condition = "and isVideoEarning='1'";
    }
    if($type == 'CP'){
        $column = "credit";
        $condition = "and isCreditPointEarning='1'";
    }
    if($type == 'DEPOSIT'){
        $column = "debit";
        $condition = "and isDeposit='1'";
    }
    if($type == 'WITHDRAW'){
        $column = "debit";
        $condition = "and isWithdraw='1'";
    }
    if($type == 'TOTAL'){
        $column = "credit";
        $condition = "and (isReferralEarning='1' or isVideoEarning='1' or isCreditPointEarning='1')";
    }
    
    $sql = "SELECT sum($column) sum FROM member_transaction_history where member_id = $member_id $condition";
    			 

    $command = Yii::app()->db->createCommand($sql);
    $sum = $command->queryScalar();
    if (empty($sum)) {
        $sum = 0;
    }
    return $sum;
}

?>
<h1>Your Balance Status</h1>
<table class="matching-table">
    <tr>
        <th>#</th>
        <th>Amount</th>            
    </tr>
    <tr>
        <td>Current Balance</td>
        <td><?php echo $this->getCurrentUserBalance($member_id); ?></td>
    </tr>
    <tr>
        <td>Referral Earnings</td>
        <td><?php echo getBalanceStatus("REFERRAL", $member_id); ?></td>
    </tr>
    <tr>
        <td>Video Earnings</td>
        <td><?php echo getBalanceStatus("VIDEO", $member_id); ?></td>
    </tr>
    <tr>
        <td>CP Matching Earnings</td>
        <td><?php echo getBalanceStatus("CP", $member_id); ?></td>
    </tr>
    <tr>
        <td>Total Earnings</td>
        <td><?php echo getBalanceStatus("TOTAL", $member_id); ?></td>
    </tr>
    <tr>
        <td>Total Deposit</td>
        <td><?php echo getBalanceStatus("DEPOSIT", $member_id); ?></td>
    </tr>
    <tr>
        <td>Total Withdraw</td>
        <td><?php echo getBalanceStatus("WITHDRAW", $member_id); ?></td>
    </tr>
</table>
